package com.vn.rainbow.exoalarm.ui;

import java.util.Calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.vn.rainbow.exoalarm.R;

public class TimeSetlectedView extends BaseView {

	private Paint mCirclePaint, mCircleFill, mTextPaint;

	private float mRadius;
	private int size;
	private int color;

	private OnClickListener mOnClickListener;

	private boolean is24HourFormat;

	private Rect bounds;

	private boolean isClick;

	private String time;
	private String am;

	public TimeSetlectedView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub

		density = getResources().getDisplayMetrics().density;

		mRadius = getResources().getDimension(R.dimen.radius_time);

		size = (int) (mRadius * 2 + 10 * density);
		setMinimumWidth(size);
		setMinimumHeight(size);

		color = getResources().getColor(R.color.color_theme);

		mCirclePaint = initPaint();
		mCirclePaint.setStrokeWidth(2 * density);
		mCirclePaint.setStyle(Style.STROKE);
		mCirclePaint.setColor(color);

		mCircleFill = initPaint();
		mCircleFill.setColor(color);
		mCircleFill.setAlpha(100);
		mCircleFill.setStyle(Style.FILL);

		mTextPaint = initPaint();
		mTextPaint.setColor(color);

		bounds = new Rect();

		time = getTime();
		am = getAM();

	}

	private String getAM() {
		if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 12) {
			return "AM";
		}
		return "PM";
	}

	private String getTime() {

		is24HourFormat = DateFormat.is24HourFormat(mContext);

		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

		if (!is24HourFormat && hour > 12) {
			hour -= 12;
		}

		if (!is24HourFormat && hour == 0)
			hour = 12;

		String str1 = Integer.toString(hour);
		if (str1.length() == 1)
			str1 = "0" + str1;

		String str2 = Integer.toString(Calendar.getInstance().get(
				Calendar.MINUTE));
		if (str2.length() == 1)
			str2 = "0" + str2;
		return str1 + ":" + str2;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		if (isClick)
			canvas.drawCircle(size / 2, size / 2, mRadius, mCircleFill);
		canvas.drawCircle(size / 2, size / 2, mRadius, mCirclePaint);

		drawTime(canvas);
	}

	private void drawTime(Canvas canvas) {

		mTextPaint.setTextSize(50 * density);
		mTextPaint.getTextBounds(time + "", 0, time.length(), bounds);
		int xPos = (int) ((size - bounds.width()) / 2);
		int yPos = (int) ((size / 2) - ((mTextPaint.descent() + mTextPaint
				.ascent()) / 2));
		canvas.drawText(time, xPos, yPos, mTextPaint);

		if (!is24HourFormat) {
			int heightTime = bounds.height();

			mTextPaint.setTextSize(25 * density);
			mTextPaint.getTextBounds(am + "", 0, am.length(), bounds);
			xPos = (int) ((size - bounds.width()) / 2);
			yPos = (int) (yPos + heightTime * 3 / 4 - ((mTextPaint.descent() + mTextPaint
					.ascent()) / 2));
			canvas.drawText(am, xPos, yPos, mTextPaint);
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			isClick = true;
			invalidate();
			return true;

		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			isClick = false;
			invalidate();
			if (this.mOnClickListener != null)
				this.mOnClickListener.onClick(this);
			break;
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void setOnClickListener(OnClickListener mOnClickListener) {
		this.mOnClickListener = mOnClickListener;
	}

	public void setChangeTime(String t, String am, boolean is24Format) {
		this.time = t;
		this.am = am;
		this.is24HourFormat = is24Format;
		invalidate();
	}

}
